package Actions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import model.StudentModel;

import org.apache.struts.action.*;

import db.DbException;


public class ChairsHomeAction extends Action 
{
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
		    HttpServletRequest request, HttpServletResponse response) 
		throws DbException
	{
		
		// Retrieve all reviewers
		RowSet crsReviewers = StudentModel.getAllReviewers();
		
		// Store the RowSet in the request scope
		request.setAttribute("crsReviewers", crsReviewers);
		
		forms.ChairsHomeForm ch = (forms.ChairsHomeForm) form;
		
		String url = "jdbc:postgresql://localhost:5432/Applications";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		int markStart = 0;
		
		try {
			// Registering Postgresql JDBC driver
			Class.forName("org.postgresql.Driver");
			// Open a connection to the database	
			conn = DriverManager.getConnection(url,"postgres","postgres");
			// Create the statement
		    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
		                				ResultSet.CONCUR_UPDATABLE);
			
	        rs = stmt.executeQuery("SELECT * FROM revProc");
	        
	        //check if the review process has started already
	        while( rs.next() )
	        {
	        	markStart = rs.getInt("started");
	        }
	        
	        if( markStart == 0 )
	        {
	        	ch.setNewReview(true);
	        }
	        else
	        {
	        	ch.setNewReview(false);
	        }
			
			// close connection
			rs.close();
			stmt.close();
			conn.close();
			
		} catch (SQLException e) 
		{ 
			System.out.println(e);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return (mapping.findForward("chairSuccess"));	
	}

}
